﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.ProcurementCommand;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.ProcurementCommandHander
{
    public class ShowProcurementCommandHander : IRequestHandler<ShowProcurementCommand, ApiResult<List<ProcurementModel>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public ShowProcurementCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<List<ProcurementModel>>> Handle(ShowProcurementCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ProcurementModel>> result = new ApiResult<List<ProcurementModel>>();

            var list = await db.Queryable<ProcurementModel>()
                                .Where(x => x.ProcurementId == request.ProcurementId)
                                .ToListAsync();

            result.Data = list;

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";

            return await Task.FromResult(result);
        }
    }
}
